Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
aggregate-error
Advanced tools
The aggregate-error package is used to create an Error instance that aggregates multiple errors into a single error object. This can be useful when you have several operations that may each throw errors, and you want to collect all of those errors into a single error object that can be thrown, returned, or logged.
Aggregating multiple errors
This feature allows you to collect multiple errors into a single error object. The code sample demonstrates how to create an instance of AggregateError with an array of Error objects and then throw it.
const AggregateError = require('aggregate-error');
const errors = [
new Error('First error'),
new Error('Second error')
];
const aggregatedError = new AggregateError(errors);
throw aggregatedError;
Iterating over aggregated errors
This feature allows you to iterate over the individual errors within an AggregateError instance. The code sample shows how to loop through each error using a for...of loop and log them to the console.
const AggregateError = require('aggregate-error');
const errors = [
new Error('First error'),
new Error('Second error')
];
const aggregatedError = new AggregateError(errors);
for (const error of aggregatedError) {
console.error(error);
}
The verror package provides a way to create rich JavaScript errors. It allows you to chain errors, add context to them, and format multi-line error messages. It is similar to aggregate-error in that it helps manage multiple error objects, but it focuses more on providing additional context and information about single errors rather than aggregating multiple errors.
Multi-error is another package that allows you to combine multiple errors into a single error object. It is similar to aggregate-error, but it has a different API and additional features such as filtering and mapping over the contained errors.
Create an error from multiple errors
Note: With Node.js 15, there's now a built-in AggregateError
type.
$ npm install aggregate-error
import AggregateError from 'aggregate-error';
const error = new AggregateError([new Error('foo'), 'bar', {message: 'baz'}]);
throw error;
/*
AggregateError:
Error: foo
at Object.<anonymous> (/Users/sindresorhus/dev/aggregate-error/example.js:3:33)
Error: bar
at Object.<anonymous> (/Users/sindresorhus/dev/aggregate-error/example.js:3:13)
Error: baz
at Object.<anonymous> (/Users/sindresorhus/dev/aggregate-error/example.js:3:13)
at AggregateError (/Users/sindresorhus/dev/aggregate-error/index.js:19:3)
at Object.<anonymous> (/Users/sindresorhus/dev/aggregate-error/example.js:3:13)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
*/
for (const individualError of error.errors) {
console.log(individualError);
}
//=> [Error: foo]
//=> [Error: bar]
//=> [Error: baz]
Returns an Error
.
Type: Array<Error|object|string>
If a string, a new Error
is created with the string as the error message.
If a non-Error object, a new Error
is created with all properties from the object copied over.
FAQs
Create an error from multiple errors
The npm package aggregate-error receives a total of 28,028,056 weekly downloads. As such, aggregate-error popularity was classified as popular.
We found that aggregate-error demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.